Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle svelte syntax #204

Merged
merged 12 commits into from
May 1, 2021
Merged

Handle svelte syntax #204

merged 12 commits into from
May 1, 2021

Conversation

Shinigami92
Copy link
Member

@Shinigami92 Shinigami92 commented Apr 22, 2021

close #201

This also affected the behavior of interpolation-token tokens and it's bracket spacing handling

@Shinigami92 Shinigami92 added type: bug Functionality that does not work as intended/expected framework: Svelte Related to the framework Svelte labels Apr 22, 2021
@Shinigami92 Shinigami92 self-assigned this Apr 22, 2021
src/printer.ts Outdated
Comment on lines 492 to 544
// Find single curly brackets for svelte
const start2: number = text.indexOf('{');
if (this.options.pugFramework === 'svelte' && start2 !== -1) {
result += text.slice(0, start2);
text = text.slice(start2 + 1);
const end2: number = text.indexOf('}');
if (end2 !== -1) {
let code: string = text.slice(0, end2);
try {
// Index of primary quote
const q1: number = code.indexOf(this.quotes);
// Index of secondary (other) quote
const q2: number = code.indexOf(this.otherQuotes);
// Index of backtick
const qb: number = code.indexOf('`');
if (q1 >= 0 && q2 >= 0 && q2 > q1 && (qb < 0 || q1 < qb)) {
logger.log({
code,
quotes: this.quotes,
otherQuotes: this.otherQuotes,
q1,
q2,
qb
});
logger.warn(
'The following expression could not be formatted correctly. Please try to fix it yourself and if there is a problem, please open a bug issue:',
code
);
result += handleBracketSpacing(this.options.pugBracketSpacing, code);
text = text.slice(end2 + 1);
continue;
} else {
code = this.frameworkFormat(code);
}
} catch (error: unknown) {
logger.warn('[PugPrinter:formatText]: ', error);
try {
code = format(code, {
parser: 'babel',
...this.codeInterpolationOptions,
semi: false
});
if (code[0] === ';') {
code = code.slice(1);
}
} catch (error: unknown) {
logger.warn(error);
}
}
code = unwrapLineFeeds(code);
result += handleBracketSpacing(this.options.pugBracketSpacing, code, ['{', '}']);
text = text.slice(end2 + 1);
} else {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section is just a stupid copy-paste of the above code, but it checks for one instead of two curly brackets...
We should refactor it and find out if there is a better solution.
Also the error handling was removed except a default warning and it's currently only checking if framework is set to svelte

We also need more test cases with text svelte interpolation. Currently I observed that the whole part in the try block is failing and always the catch fallback algorithm is taken 🤔.
Therefore can it removed for svelte, or are there possibilities where it's useful?

src/printer.ts Show resolved Hide resolved
src/utils/common.ts Outdated Show resolved Hide resolved
src/utils/svelte.ts Show resolved Hide resolved
src/printer.ts Outdated Show resolved Hide resolved
@Shinigami92
Copy link
Member Author

@GrygrFlzr @basaran @dummdidumm
hey 😃
Could you have a look over this PR?
This a first working solution for svelte 🎉
Maybe someone of you could try it out in a production environment? With yarn link or patch-package?

@Shinigami92 Shinigami92 marked this pull request as ready for review April 28, 2021 20:16
@@ -393,6 +394,7 @@ export class PugPrinter {
private formatText(text: string): string {
let result: string = '';
while (text) {
// Find double curly brackets
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor this entire method into smaller parts (that can be reused and are easier to grasp)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, this code hurts, but I personally decided for now to await feedback from svelte community if there are further errors and printed warnings in their console output
Based on this feedback I can refactor this messy code
But now it's working and the svelte community can start working our formatter

src/printer.ts Show resolved Hide resolved
src/utils/svelte.ts Outdated Show resolved Hide resolved
src/utils/angular.ts Outdated Show resolved Hide resolved
Shinigami92 and others added 2 commits May 1, 2021 18:09
Co-authored-by: ST-DDT <ST-DDT@gmx.de>
src/utils/svelte.ts Show resolved Hide resolved
@Shinigami92 Shinigami92 closed this May 1, 2021
@Shinigami92 Shinigami92 reopened this May 1, 2021
@Shinigami92 Shinigami92 merged commit c0e5396 into main May 1, 2021
@Shinigami92 Shinigami92 deleted the issue-201 branch May 1, 2021 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
framework: Svelte Related to the framework Svelte type: bug Functionality that does not work as intended/expected
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: svelte pug formatting
2 participants